<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <!-- build:css -->
    <link rel="stylesheet" href="/static/lib/kitadmin/css/layui.css" th:href="@{/lib/kitadmin/css/layui.css}">
    <link rel="stylesheet" href="/static/lib/kitadmin/css/theme/default.css" th:href="@{/lib/kitadmin/css/theme/default.css}" id="theme">
    <link rel="stylesheet" href="/static/lib/kitadmin/css/nprogress.css" th:href="@{/lib/kitadmin/css/nprogress.css}">
    <!-- endbuild -->
</head>
<body>
<div class="container">
    <form class="layui-form" id="form-user-edit" th:object="${user}" lay-filter="form-user">
        <input name="userId"  type="hidden"  th:field="*{userId}" />
        <input name="deptId"  type="hidden"  th:field="*{deptId}" id="treeId"/>
        <div class="layui-form-item">
            <label class="layui-form-label">登录名称：</label>
            <div class="layui-input-block">
                <input class="layui-input" type="text" readonly="true" th:field="*{loginName}"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门名称：</label>
            <div class="layui-input-block">
                <input class="layui-input" type="text" name="deptName" onclick="selectDeptTree()" readonly="true" id="treeName" th:field="*{dept.deptName}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名称：</label>
            <div class="layui-input-block">
                <input class="layui-input" type="text" name="userName" id="userName" th:field="*{userName}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱：</label>
            <div class="layui-input-block">
                <input class="layui-input" type="text" name="email" id="email" th:field="*{email}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机：</label>
            <div class="layui-input-block">
                <input class="layui-input" type="text" name="phonenumber" id="phonenumber" th:field="*{phonenumber}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别：</label>
            <div class="layui-input-block">
                <select id="sex" lay-verify="required" lay-filter="sex" th:with="type=${@dict.getType('sys_user_sex')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{sex}"></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态：</label>
            <div class="layui-input-block">
                <div class="onoffswitch">
                    <input type="checkbox" th:checked="${user.status == '0' ? false : true}" id="status" name="status"
                           lay-skin="switch">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">岗位：</label>
            <div class="layui-input-block">
                <th:block th:each="post,inte:${posts}" class="check-box">
                    <input th:name="'post['+${inte.index}+']'" type="checkbox" lay-skin="primary"  th:value="${post.postId}" th:title="${post.postName}" th:checked="${post.flag}" th:disabled="${post.status == '1'}">
                </th:block>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色：</label>
            <div class="layui-input-block">
                <th:block th:each="role,inte:${roles}" class="check-box">
                    <input th:name="'role['+${inte.index}+']'" type="checkbox" lay-skin="primary"  th:value="${role.roleId}" th:title="${role.roleName}" th:checked="${role.flag}" th:disabled="${role.status == '1'}">
                </th:block>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="btn-save">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>

    </form>
</div>
<script th:inline="javascript"> var ctx = [[@{/}]]; </script>

<script src="../static/lib/kitadmin/polyfill.min.js" th:src="@{/lib/kitadmin/polyfill.min.js}"></script>
<script src="../static/lib/kitadmin/layui.js" th:src="@{/lib/kitadmin/layui.js}"></script>
<script src="../static/lib/kitadmin/kitadmin.js" th:src="@{/lib/kitadmin/kitadmin.js}"></script>
<script type="text/javascript">
    layui.config({
        base: ctx + 'js/'
    }).use(['element','layer','form','common','jquery'],function(){
        var $ = layui.$
            ,element = layui.element
            ,layer = layui.layer
            ,form = layui.form
            ,common = layui.common;

        var index = parent.layer.getFrameIndex(window.name);

        form.render(null, 'form-user');
        form.on('submit(btn-save)', function(data){


            var roleIds = common.getCheckValues("role");
            var postIds = common.getCheckValues("post");
            data.field.roleIds = roleIds;
            data.field.postIds = postIds;
            data.field.status = data.field.status === "on" ? "1":"0";

            console.log(JSON.stringify(data.field));

            //TODO:提供一个检测表单数据检测性通用方法
            if(true){
                $.ajax({
                    cache : false,
                    type : "POST",
                    url : ctx + "system/user/edit",
                    data : data.field,
                    async : false,
                    error : function(request) {
                        layui.msg("系统错误");
                    },
                    success : function(data) {
                        console.log("success:",data);
                        if(data.code > 0 ){
                            layui.msg(data.msg);
                        }else{
                            parent.layer.close(index);
                        }
                    }
                });
            }
            return false;
        });
    });
</script>
</body>
</html>
